package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopSysInvoiceBO;
import com.ztgf.shopping.models.bo.shop.ShopSysInvoiceQueryBO;
import com.ztgf.shopping.models.service.shop.IShopSysInvoiceService;
import com.ztgf.shopping.models.vo.shop.ShopSysInvoiceApplyVO;
import com.ztgf.shopping.models.vo.shop.ShopSysInvoiceRelevanceVO;
import com.ztgf.shopping.models.vo.shop.ShopSysInvoiceVO;
import com.ztgf.sys.models.bo.SysInvoiceBO;
import com.ztgf.sys.models.bo.SysInvoiceQueryBO;
import com.ztgf.sys.models.dto.SysInvoiceDTO;
import com.ztgf.sys.models.service.ISysInvoiceService;
import com.ztgf.sys.models.vo.SysInvoiceQueryVO;
import com.ztgf.sys.models.vo.SysInvoiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 店铺平台开票控制器
 *
 * @author liyang
 * @Date 2021-05-26 12:08:14
 */

@RestController
@RequestMapping("/api/shopSysInvoice")
@Slf4j
@Api(value = "ShopSysInvoiceApiController",tags = "店铺平台开票Api接口")
public class ShopSysInvoiceApiController {

    @DubboReference
    private IShopSysInvoiceService shopSysInvoiceService;
    @DubboReference
    private ISysInvoiceService sysInvoiceService;



    @ApiOperation(value = "申请平台开票列表",notes = "申请平台开票列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopSysInvoiceVO>> queryPageList(ShopSysInvoiceBO bo){
        return new Result<> (shopSysInvoiceService.queryPageList(bo));
    }

    @ApiOperation(value = "平台开票记录",notes = "平台开票记录")
    @PostMapping("/queryPageListByRecord")
    public Result<Page<SysInvoiceVO>> queryPageListByRecord(SysInvoiceQueryBO bo){
        return new Result<> (sysInvoiceService.queryPageList(bo));
    }

    @PostMapping("/get")
    @ApiOperation(value = "查看开票详情",notes = "查看开票详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysInvoiceQueryVO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        SysInvoiceQueryVO sysInvoiceQueryVO = new SysInvoiceQueryVO ();
        SysInvoiceDTO dto = sysInvoiceService.selectById(id);
        List<ShopSysInvoiceRelevanceVO> shopSysInvoiceRelevanceVOS = shopSysInvoiceService.selectVOS (id);
        sysInvoiceQueryVO.setSysInvoiceDTO (dto);
        sysInvoiceQueryVO.setShopSysInvoiceRelevanceVOS (shopSysInvoiceRelevanceVOS);
        return new Result<>(sysInvoiceQueryVO);
    }

    @ApiOperation(value = "申请开票展示",notes = "申请开票展示")
    @PostMapping("/applyInvoice")
    public Result<ShopSysInvoiceApplyVO> applyInvoice(ShopSysInvoiceQueryBO bo){
        if(bo == null || bo.getShopId () == null || bo.getPriceType () == null || bo.getMoldType () == null){
            return new Result<> (CodeEnum.FAIL_100002);
        }
        return  shopSysInvoiceService.applyInvoice(bo);
    }


    @ApiOperation(value = "申请平台开票",notes = "申请平台开票")
    @PostMapping("/saveInvoice")
    public Result<?> saveInvoice(SysInvoiceBO bo){
        if(bo == null || bo.getShopId () == null || bo.getInvoiceHeaderId () == null
            || bo.getInvoiceType () == null || bo.getPriceType () == null
            || bo.getInvoicePrice () == null ){
            return new Result<> (CodeEnum.FAIL_100002);
        }
        boolean flag = sysInvoiceService.saveInvoice(bo);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }


}
